clear
cap frame change default
cap frame drop results
frame create results strL(label) b_evict se_evict b_evictXout se_evictXout n_persons n_person_quarters non_evict_base

local outcomelist1="fin_health VANTAGE_V3_SCORE ANY_AUT_OR_LEASE ALL5042 zerorevolving "

** Regressions
foreach per in "1_4" "5_8" {

	if "`per'"=="1_4" local did_est="18_21"
	if "`per'"=="5_8" local did_est="22_25"
	
	if "`per'"=="1_4" local base="13_16"
	if "`per'"=="5_8" local base="9_12"
	
	use unique_id judgeXyear `outcomelist1' exp_indiv_id filing_date  exp_quarter evicted qsince monthsince archive_year using "${build}/exp_analysis.dta"  if qsince>=-16 & qsince<=24, clear
	
	* Variables * 
	qui tab qsince, gen(qdum)
	foreach num of numlist 1(1)41 {
			gen evictXq`num'=evicted*qdum`num'
	}

	* Construct outcome windows
	foreach wind in "18_21" "22_25"  ///
				"13_16" "9_12" {
		local f=substr("`wind'",1,2)
		local l=substr("`wind'",4,2)
		
		if "`wind'"=="9_12"  {
					local f=substr("`wind'",1,1)
					local l=substr("`wind'",3,2)
		}
		
		egen qdum`f'_`l'=rowtotal(qdum`f'-qdum`l')
		gen evictXq`f'_`l'=evicted*qdum`f'_`l'
	}

	fvset base 2011 archive_year
	
	foreach var of varlist `outcomelist1' {
				local vname=`"`:variable label `var''"'	
				
				if "`per'"=="1_4" {
					areg `var' evicted qdum1-qdum12 qdum17 qdum18_21 qdum22-qdum41 ///
									   evictXq1-evictXq12 evictXq17 evictXq18_21 evictXq22-evictXq41 `controls' i.exp_quarter, a(exp_indiv_id) cluster(exp_indiv_id) robust
				}
				
				if "`per'"=="5_8" {
					areg `var' evicted qdum1-qdum8 qdum13-qdum21 qdum22_25 qdum26-qdum41 ///
									   evictXq1-evictXq8 evictXq13-evictXq21 evictXq22_25 evictXq26-evictXq41 `controls' i.exp_quarter, a(exp_indiv_id) cluster(exp_indiv_id) robust
				}
				
				local label="Pop: All, Outcome: `vname', Outcome period: `per'"
				
				*Save regression output:
				local b_evicted=_b[evicted]
				local se_evicted=_se[evicted]
				local b_evictXout=_b[evictXq`did_est']
				local se_evictXout=_se[evictXq`did_est']
				
				sum `var' if evicted==0 & qdum`base'==1 & e(sample)
				local non_evict_base_mean=r(mean)
				local n_person_quarters=e(N)
				capture drop first 
				capture drop sample
				gen sample=e(sample)
				bys exp_indiv_id sample: gen first=_n==1
				count if first==1 & sample==1
				local n_persons=.
				
				frame post results ("`label'") (`b_evicted') (`se_evicted') (`b_evictXout') (`se_evictXout') ///
				(`n_persons') (`n_person_quarters') (`non_evict_base_mean')
				
				frame results: export delimited using "${Inputs}/Cook_credit_did.csv", replace
				}
}
